extends Control
@onready var health_bar: ProgressBar = %血条
@onready var hp_label: Label = %hpLabel
@onready var magic_bar: ProgressBar = %蓝条
@onready var mp_label: Label = %mpLabel
@onready var exp_bar: ProgressBar = %经验
@onready var boss_health_bar: ProgressBar = %Boss血条
@onready var time: Label = %time
@onready var skills: HBoxContainer = %Skills
@onready var 金币: Label = %金币
@onready var grid_container: GridContainer = %GridContainer

var player_stats: PandoraEntity
func _ready() -> void:
	player_stats = Utils.player.player_stats
	player_stats.changed.connect(_on_player_stats_changed)
	_on_player_stats_changed()
	boss_health_bar.value = 0
	Utils.mouse_ignore(self)
	## 绑定资源变化
	EventBus.subscribe("boss_hp_change", on_boss_hp_change)
	EventBus.subscribe("player_skill_change", on_player_skill_change)
	EventBus.subscribe("show_fluent_tip", on_show_fluent_tip)
	EventBus.subscribe("show_time", on_show_time)

	time.visible = false
	clear_skill()

## 显示时间，倒计时
func on_show_time(value, visiable):
	time.text = str(value) + "秒"
	time.visible = visiable

## 玩家相关属性改变
func _on_player_stats_changed():
	health_bar.max_value = player_stats.get_float("max_hp")
	health_bar.value = player_stats.get_float("current_hp")
	hp_label.text = var_to_str(player_stats.get_float("current_hp")) + "/" + var_to_str(player_stats.get_float("max_hp"))
	magic_bar.max_value = player_stats.get_float("max_mp")
	magic_bar.value = player_stats.get_float("current_mp")
	mp_label.text = var_to_str(player_stats.get_float("current_mp")) + "/" + var_to_str(player_stats.get_float("max_mp"))
	exp_bar.max_value = player_stats.get_float("level") * 100
	exp_bar.value = player_stats.get_float("exp")
	金币.text = str(player_stats.get_integer("gold"))
	for skill in skills.get_children():
		skill.current_cold_time = skill.origin_cold_time * (1 - player_stats.get_float("cooldown_reduction"))
		
func on_player_skill_change(_player_skill_list: Array):
	var skill_array = skills.get_children()
	var index = 0
	clear_skill()
	var key_name = 1
	for _skill: PandoraEntity in _player_skill_list:
		var skill: SkillButton = skill_array[index]
		skill.visible = true
		skill.skill_name.text = _skill.get_string("name")
		## 所有技能的蓝耗都随着等级n乘n倍
		var skill_level = _skill.get_integer("level")
		var base_mp_cost = _skill.get_integer("need_mp")
		if base_mp_cost >= 0:  # 只对主动技能（蓝耗>=0）应用等级倍数
			skill.need_mp = base_mp_cost * skill_level
		else:
			skill.need_mp = base_mp_cost  # 被动技能保持原值
		skill.skill_pandora_entity = _skill
		skill.origin_cold_time = _skill.get_float("cool_down")
		skill.current_cold_time = skill.origin_cold_time * (1 - player_stats.get_float("cooldown_reduction"))
		skill.skill_image.set_texture(_skill.get_resource("skill_image"))
		skill.skill_func = SkillManager.get_skill_func(_skill.get_string("name"), _skill.get_integer("level"))
		skill.skill_index = "skill%s" % key_name
		## 如果是冲刺，就绑定shift
		if skill.skill_name.text == "冲刺":
			skill.bind_key("shift")
		else:
			## 如果是主动技能，才绑定按键
			if skill.need_mp >= 0:
				skill.bind_key(str(key_name))
				key_name += 1
			else:
				skill.bind_key()
		index += 1

func clear_skill():
	var skill_array = skills.get_children()
	for array in skill_array:
		array.visible = false
		
func on_boss_hp_change(hp, max_hp):
	# 若是分裂型Boss（史莱姆），聚合所有史莱姆的血量
	var slimes = get_tree().get_nodes_in_group("boss_slime")
	if slimes.size() > 0:
		var total_hp: float = 0.0
		var total_max: float = 0.0
		for s in slimes:
			if s is Slime:
				var ec: EnemyHealthComponent = s.enemy_health_component
				if ec:
					total_hp += max(ec.health, 0.0)
					total_max += s.get_stats().get_float("max_health")
		boss_health_bar.max_value = total_max
		boss_health_bar.value = clamp(total_hp, 0.0, total_max)
		return
	# 默认行为
	boss_health_bar.max_value = max_hp
	boss_health_bar.value = hp

## 限制最多10条信息
func on_show_fluent_tip(fluent_tip):
	if grid_container.get_children().size() < 10:
		grid_container.add_child(fluent_tip)
